package com.dengyuanke.admin.controller;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Carousel;
import com.dengyuanke.service.ICarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 轮播图
 * Created by dengyuanke on 2017/11/13.
 */
@Api(tags = "轮播图相关API")
@RestController
public class CarouselController {

    @Autowired
    private ICarouselService carouselService;

    @ApiOperation(value = "添加轮播图",notes = "传入轮播图实体")
    @PostMapping("/addcarousel")
    public JsonResult addCarousel(Carousel carousel){
        carousel.setId(IdWorker.get32UUID());
        carousel.setCreateTime(new Date());
        boolean insert = carouselService.insert(carousel);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    @ApiOperation(value = "更新轮播图",notes = "传入带id轮播图实体")
    @PostMapping("/updatecarousel")
    public JsonResult updateCarousel(Carousel carousel){
        if(StringUtils.isEmpty(carousel.getId())){
            return JsonResult.renderFail("未传入实体id，更新失败");
        }
        carousel.setUpdateTime(new Date());
        boolean insert = carouselService.insert(carousel);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    @ApiOperation(value = "获取轮播图",notes = "不传入参数")
    @PostMapping("/getcarousels")
    public JsonResult getCarousels(){
       return JsonResult.renderSuccess(carouselService.selectList(null));
    }
}
